package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.knowledge.domain.bo.KnowledgeVectorBo;
import org.dromara.knowledge.domain.vo.KnowledgeVectorVo;
import org.dromara.knowledge.service.KnowledgeVectorService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库向量控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/vector")
public class KnowledgeVectorController extends BaseController {

    private final KnowledgeVectorService knowledgeVectorService;

    /**
     * 查询知识库向量列表
     */
    @SaCheckPermission("knowledge:vector:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeVectorVo> list(KnowledgeVectorBo bo, PageQuery pageQuery) {
        return knowledgeVectorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库向量列表
     */
    @SaCheckPermission("knowledge:vector:export")
    @Log(title = "知识库向量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeVectorBo bo, HttpServletResponse response) {
        List<KnowledgeVectorVo> list = knowledgeVectorService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库向量", KnowledgeVectorVo.class, response);
    }

    /**
     * 获取知识库向量详细信息
     *
     * @param vectorId 向量ID
     */
    @SaCheckPermission("knowledge:vector:query")
    @GetMapping("/{vectorId}")
    public R<KnowledgeVectorVo> getInfo(@PathVariable Long vectorId) {
        return R.ok(knowledgeVectorService.queryById(vectorId));
    }

    /**
     * 新增知识库向量
     */
    @SaCheckPermission("knowledge:vector:add")
    @Log(title = "知识库向量", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeVectorBo bo) {
        return toAjax(knowledgeVectorService.insertByBo(bo));
    }

    /**
     * 修改知识库向量
     */
    @SaCheckPermission("knowledge:vector:edit")
    @Log(title = "知识库向量", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeVectorBo bo) {
        return toAjax(knowledgeVectorService.updateByBo(bo));
    }

    /**
     * 删除知识库向量
     *
     * @param vectorIds 向量ID数组
     */
    @SaCheckPermission("knowledge:vector:remove")
    @Log(title = "知识库向量", businessType = BusinessType.DELETE)
    @DeleteMapping("/{vectorIds}")
    public R<Void> remove(@PathVariable Long[] vectorIds) {
        return toAjax(knowledgeVectorService.deleteWithValidByIds(List.of(vectorIds), true));
    }

    /**
     * 根据片段ID查询向量列表
     *
     * @param fragmentId 片段ID
     */
    @SaCheckPermission("knowledge:vector:query")
    @GetMapping("/fragment/{fragmentId}")
    public R<List<KnowledgeVectorVo>> getByFragmentId(@PathVariable String fragmentId) {
        return R.ok(knowledgeVectorService.queryByFragmentId(fragmentId));
    }

    /**
     * 根据知识库ID查询向量列表
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:vector:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<List<KnowledgeVectorVo>> getByKnowledgeId(@PathVariable String knowledgeId) {
        return R.ok(knowledgeVectorService.queryByKnowledgeId(knowledgeId));
    }
}